layui.define(['jquery', 'form', 'layer', 'element'], function (exports) {

    var $ = layui.jquery
        , form = layui.form
        , layer = layui.layer
        , element = layui.element;

    var inputtags = {

        render: function ($params) {
            var rec = [],
                str = '',
                HiddenValue='',
                url = $params.url ? $params.url:'',
                inner = $('inner' in $params && $params.inner ? $params.inner : '.Ms-labe-inner'),//已选标签box
                innerHidden = $('innerHidden' in $params && $params.innerHidden ? $params.innerHidden :'.Ms-labe-innerHidden'),//所以标签数据（隐藏输入框）
                addbtn = $('addbtn' in $params && $params.addbtn ? $params.addbtn :'.Ms-labe-addbtn'),//添加按钮
                addinput = $('addinput' in $params && $params.addinput ? $params.addinput :'.Ms-labe-addinput'),//添加输入框
                backstage = $( 'backstage' in $params && $params.backstage ? $params.backstage :'.Ms-labe-backstage'),//推荐标签
                labebackstage=$('labebackstage' in $params && $params.labebackstage ? $params.labebackstage :'.Ms-labe-backstage')//快捷选项
            ;

            inner.on('click', '.Ms-labe-Remove', function () {
                var t = $(this), em=$(this).data('id'), val = $.trim(innerHidden.val()).split(',');
                for (var x = 0; x < val.length; x++) {
                    if (val[x] == em) {
                        val.splice(x, 1);
                        x--;
                    }
                }
                val = val.join(',');
                innerHidden.val(val === ',' ? '' : val);
                t.parents('.Ms-labe-box').remove();
                return false;
            });
            //已选标签点击
            backstage.on('click', labebackstage , function () {
                HiddenValue=$(this).data('id');//
                if(!HiddenValue){
                    HiddenValue=-1;
                }
                run($.trim($(this).html()));
                return false;
            });
            //添加标签
            addbtn.on('click', function () {
                var val = $.trim(addinput.val());
                if (val !== '') {
                    if(url){
                        data={
                            'val':val
                        };
                        var index='';
                        $.ajax({
                            method:"post",
                            url: url, //用于文件上传的服务器端请求地址
                            data: data,
                            beforeSend(){
                                index=layer.load(1, {
                                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                                });
                                $(addbtn).attr('disabled',"disabled");
                            },
                            success(data){
                                id=data.data.id;
                                if(data.code==200){
                                   innerHidden=id;
                                }
                            },
                            complete: function () {
                                $(addbtn).removeAttr("disabled");
                                layer.close(index);
                            },
                            error(data){
                                console.info("error: " + data.responseText);
                            }

                        })
                    }else{
                        innerHidden=-1;
                    }
                    run($.trim(val), true);
                } else {
                    //内容不能为空！
                    layer.msg('输入的内容不能为空');
                }
                return false;
            });
            function run(data, type) {
                var val = $.trim(innerHidden.val()), s = val.split(','), id = true;
                $.each(s, function (i, name) {
                    if (name === data) {
                        return id = false;
                    }
                });
                if (id) {
                    if (s.length < 5) {
                        if(HiddenValue==-1){
                            innerHidden.val(val += ((val ? ',' : '') + val));
                        }else{
                            innerHidden.val(val += ((val ? ',' : '') + HiddenValue));
                        }
                        inner.append('<button type="button" class="layui-btn layui-btn-sm layui-btn-normal Ms-labe-box"><em>' + data + '</em><i class="layui-icon layui-icon-close Ms-labe-Remove" data-id="'+HiddenValue+'" style="color:red">X</i></button>');
                        if (type) {
                            addinput.val('');
                        }
                    } else {
                        //最多5个
                        layer.msg('最多可用5个');
                    }
                } else {
                    //标签已存在！
                    layer.msg('该标签已经添加过了');
                }


                return id;
            }
        }
    };

    exports('inputtags', inputtags);
});